package com.supor.aiot.food.ui.food;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.food.R;
import com.supor.aiot.food.adapter.SharedRecipe1Adapter;
import com.supor.aiot.food.api.SearchFoodImpl;
import com.supor.aiot.food.bean.RecipeHotBean;
import com.supor.aiot.food.bean.TopicDetailBean;
import com.supor.aiot.food.view.OneItemDecoration;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration;

import java.util.ArrayList;
import java.util.List;


public class SharedRecipeThreeFragment extends Fragment {

    SwipeRecyclerView rvSharedImg;
    TextView tvSharedTitle;
    TextView tvSharedContent;
    NestedScrollView scrollView;
    SmartRefreshLayout refreshLayout;

    private List<RecipeHotBean> mlist = new ArrayList<>();
    private TopicDetailBean recipeTopicBean;
    private SharedRecipe1Adapter adapter;
    private SharedRecipeActivity sharedRecipeActivity;
    private String dataID;
    private int originPage = 1;
    private ImageView ivLoad;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shared_recipe_type3, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        sharedRecipeActivity = (SharedRecipeActivity) getActivity();
        dataID = sharedRecipeActivity.getDataID();
        if (!TextUtils.isEmpty(dataID)) getTopicRecipe(dataID);
        recipeTopicBean = sharedRecipeActivity.getTopicDetail();
        if (recipeTopicBean != null && !TextUtils.isEmpty(recipeTopicBean.getCuisineTitel())) {
            tvSharedTitle.setText(recipeTopicBean.getCuisineTitel());
        }
        if (recipeTopicBean != null && !TextUtils.isEmpty(recipeTopicBean.getCuisineDescribe()))
            tvSharedContent.setText(recipeTopicBean.getCuisineDescribe());
        adapter = new SharedRecipe1Adapter(R.layout.item_shared_recipe, mlist);
        adapter.setDescVisiable(true);
        adapter.setOnItemClickListener((adapter, view, position) -> AppAgent.startRecipePage(mlist.get(position).getRecipeId(), mlist.get(position).getModelId()));
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvSharedImg.setLayoutManager(layoutManager);
        rvSharedImg.addItemDecoration(new OneItemDecoration(getActivity()));
        rvSharedImg.setAdapter(adapter);
    }

    @SuppressLint("AutoDispose")
    private void getTopicRecipe(String dataId) {
        ivLoad.setVisibility(View.VISIBLE);
        SearchFoodImpl.getInstance().getTopicRecipe(dataId, originPage, 10).subscribe(result -> {
            ivLoad.setVisibility(View.GONE);
            if (result.isSuccess()) {
                if (originPage == 1) mlist.clear();
                if (mlist.size() < result.total) {
                    mlist.addAll(result.data.rows);
                } else {
                    View footerView = getLayoutInflater().inflate(R.layout.item_nomore, rvSharedImg, false);
                    if (mlist.size() > 0 && rvSharedImg.getFooterCount() == 0)
                        rvSharedImg.addFooterView(footerView);
                }
                adapter.notifyDataSetChanged();
            } else {
                Log.e("SharedRecipeOneFragment", "Recipe error");
            }
            refreshLayout.finishLoadMore();
        }, throwable -> {
            refreshLayout.finishLoadMore();
            Log.e("SharedRecipeOneFragment", "Recipe查询失败: " + throwable.getMessage());
        });
    }

    private void initView(View view) {
        rvSharedImg = view.findViewById(R.id.rv_shared_tv_img);
        tvSharedTitle = view.findViewById(R.id.tv_shared_title);
        tvSharedContent = view.findViewById(R.id.tv_shared_content);
        scrollView = view.findViewById(R.id.nest_scroll);
        refreshLayout = view.findViewById(R.id.refreshLayout);
        ivLoad = view.findViewById(R.id.iv_load);

        refreshLayout.setEnableRefresh(false);

        scrollView.setOnScrollChangeListener((View.OnScrollChangeListener) (view1, i, i1, i2, i3) -> {
            if (i1 > 150) {
                sharedRecipeActivity.showTitle();
            } else {
                sharedRecipeActivity.hideTitle();
            }
        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            originPage++;
            getTopicRecipe(dataID);
        });
        refreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));
    }


}
